package com.myBatis.JDBC;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.myBatis.JDBC.entity.RuleEntity;
import com.myBatis.JDBC.mapper.RuleMapper;

public class Test {
	
	private SqlSession sqlSession = null;
	private RuleMapper ruleMapper = null;
	
	private static SqlSessionFactory sessionFactory;
	private static final String loaction="com/myBatis/JDBC/myBatisConfig.xml";
	static{
		try {
			InputStream stream=Resources.getResourceAsStream(loaction);
			sessionFactory=new SqlSessionFactoryBuilder().build(stream);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static SqlSessionFactory getSession(){
		return sessionFactory;
	}

    public void deleteBatch() {
        SqlSession sqlSession=null;
        try {
        	SqlSession session=sessionFactory.openSession();
            RuleMapper ruleVo = sqlSession.getMapper(RuleMapper.class);
          /*  List<Long> list=new ArrayList<>();
            list.add(1L);
            list.add(2L);*/
            ruleVo.deleteRule(Arrays.asList(57L,58L,59L,60L));
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
	public void insertBatch() {
		System.out.println("批量插入");
		String parentNo = "0";
		String ruleNo = UUID.randomUUID().toString();
		String ruleName = ruleNo+"规则";
		RuleEntity parentRule = new RuleEntity(parentNo,ruleNo,ruleName);
		
		
		ruleNo = UUID.randomUUID().toString();
		ruleName = ruleNo+"规则";
		RuleEntity childOneRule = new RuleEntity(parentNo,ruleNo,ruleName);
		ruleNo = UUID.randomUUID().toString();
		ruleName = ruleNo+"规则";
		RuleEntity childTwoRule = new RuleEntity(parentNo,ruleNo,ruleName);
		
		ruleMapper.addRule(Arrays.asList(parentRule,childOneRule,childTwoRule));
	}
	public void getByRuleCodeBatch() {
		List<RuleEntity> ruleList = ruleMapper.getRuleById(Arrays.asList("DIAG","DIAG_10002","DIAG_10001"));
		ruleList.forEach(System.out::println);
	}
	
	

}
